app.controller('widthDrawCtrl', function($element, ngDialog ,$scope, $state, rest, user,uploadConf,tipService,uiLoad,$localStorage) {
    var data = angular.copy($scope.ngDialogData, {});

    $scope.params = {};
    $scope.params2 = {};    
    $scope.user = {
        phone: user.get('phone'),
        PasswordType : 1
    }
    $scope.isTransPasswordSet = user.get('isTransPasswordSet');
    $scope.bankName = user.get('bankName');
    $scope.bankAccount = user.get('bankAccount');
    $scope.bankAccountName = user.get('bankAccountName');
    console.log($scope.user); 
       function json2str(o) { 
        var arr = []; 
        var fmt = function(s) { 
        if (typeof s == 'object' && s != null) return json2str(s); 
        return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s; 
        } 
        for (var i in o) arr.push("'" + i + "':" + fmt(o[i])); 
        return '{' + arr.join(',') + '}'; 
      } 

    rest.post({
      methodId: 'J018',         
       data: {'accountOpenId': user.get('openId')},
     // data: {'accountOpenId': '1dca25eabc753b7087ef659a5d7c5c98'},
      success: function(res) {
         console.log(res);
         var numAmount = res.data.realAmount.substring(0,res.data.realAmount.length -2);    
         $scope.totalAmount = numAmount;
         $scope.totalAmountLeft = numAmount;
         $scope.$apply();
      },
      error: function() {
         
      }
    })
    $scope.$watch('withdrawAmout',function(){
      if ($scope.withdrawAmout < 0) {
        $scope.withdrawAmout = 0.01;
      }
    });
    //点击确定按钮
    $scope.modifySure = function() {
      if (!$scope.withdrawAmout || $scope.withdrawAmout == 0 ) {
        tipService.show("body", {
            message: "请输入有效金额",
            notice: "danger"
        }, 3);
        return false;
      } 
      if ($scope.withdrawAmout > $scope.totalAmount) {
        tipService.show("body", {
            message: "超过提现金额",
            notice: "danger"
        }, 3);
        $scope.amoutReady = 0;
      }else{
        $scope.amoutReady = 1;
      }     
      if ($scope.isTransPasswordSet) {
        $scope.isTransPasswordSet0 = 0;
      }else{
        $scope.isTransPasswordSet0 = 1;
        return false;
      }
    }
    $scope.withdrawSure = function() {
        $scope.params.LandlordId = user.get('landlordId');
        $scope.params.TransPassword = $scope.transPassword;
        rest.post({
          methodId: '5020',
          data: $scope.params,
          success: function(res) {
            if(res.code==0){
                  $scope.params.bankAccountName = $scope.bankAccountName;
                  $scope.params.WithdrawAmount = $scope.withdrawAmout;
                  $scope.params.bankName = $scope.bankName;
                  $scope.params.bankAccount = $scope.bankAccount;
                  $scope.params.OpenId = user.get('openId');
                  // $scope.params.OpenId = '1dca25eabc753b7087ef659a5d7c5c98';
                  $scope.params.Phone = user.get('phone'),                                 
                  rest.post({
                      methodId: '1046',
                      data: $scope.params,
                      success: function(res) {           
                        $scope.params2.projectNo = 'ZG';
                         $scope.params2.projectOrderNo = res.res.data.orderId;
                         $scope.params2.description = '账户充值';
                          $scope.params2.accountOpenId = res.res.data.openId;
                         $scope.params2.withdrawMoney = res.res.data.withdrawAmount;
                          $scope.params2.bankOpenName =res.res.data.bankName;
                          $scope.params2.bankAccount = res.res.data.bankAccount;
                          $scope.params2.bankAccountName = res.res.data.bankAccountName;
                           $scope.str = '' + 'projectNo=' + $scope.params2.projectNo + '&' + 'projectOrderNo=' + $scope.params2.projectOrderNo + '&' + 'description=' + $scope.params2.description + '&' + 'accountOpenId=' + $scope.params2.accountOpenId + '&' + 'withdrawMoney=' + $scope.params2.withdrawMoney + '&' + 'bankOpenName=' + $scope.params2.bankOpenName + '&' + 'bankAccount=' + $scope.params2.bankAccount + '&' + 'bankAccountName=' + $scope.params2.bankAccountName +'';
                        console.log($scope.str);
                      rest.post({
                              methodId: 'J021',
                              data: $scope.params2,
                              success: function(res) {
                               ngDialog.openConfirm({
                                  template: 'js/module/fund/withdrawSuccess.html',
                                  resolve: {
                                    load: function() {
                                        return uiLoad.load(['js/module/fund/withdrawSuccess.js'])
                                    }
                                  },
                                  data:{
                                  },
                                  scope: $scope,
                                  width: 600,
                                  // height: 700
                                }).then(function(data) {

                                }, function(reason) {

                                });
                              
                              },
                              error: function() {}
                          })
                      },
                      error: function() {}
                  })
            }else if(res.code==410){
                tipService.show("body", {
                    message: res.res.data.msg,
                    notice: "danger"
                }, 3);
            }
            console.log(data);
          },
          error: function() {}
      })
        
     
        
    }
    
    $scope.SetPassword = function() {
      $state.go('app.houseOwner.goCapitalPsw');     
      $scope.confirm({});
    }

    $scope.$watch('newPassword', function(newValue, oldValue) {
        $scope.user.newPassword = newValue;
    })
    $scope.$watch('comfirmedPassword', function(newValue, oldValue) {
        $scope.user.comfirmedPassword = newValue;
    })
    $scope.saveData = function() {
      if ($scope.newPassword != $scope.comfirmedPassword) {
          tipService.show("body", {
              message: "两次密码输入不一致",
              notice: "danger"
          }, 3);
          return;
      }
      if ($scope.newPassword.length != 6) {
            tipService.show("body", {
                message: "需要输入6位资金密码",
                notice: "danger"
            }, 3);
            return;
      }
      rest.post({
            methodId: '5018',
            data: $scope.user,
            success: function(res) {
                if (res.code == 0) {
                    tipService.show("body", {
                        message: "密码创建成功"
                    }, 3);
                    $localStorage.user.isTransPasswordSet = 1;
                    $scope.isTransPasswordSet = 1;
                    $scope.isTransPasswordSet0 = 0;
                } else {
                    tipService.show("body", {
                        message: "密码创建失败!",
                        notice: "danger"
                    }, 3);
                }
            },
            error: function() {

            }
        })
    }

});